﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using Cosmetic.DataEntity;

namespace WebApp.Product
{
    public partial class ProductTopCategory : System.Web.UI.Page
    {
        ProductManager PM = new ProductManager();
        private CategoryManager m_CategoryManager = new CategoryManager();
        private PlateManager pm = new PlateManager();
        private articleManger articlemanger = new articleManger();
        private articleclassManger articleClassManager = new articleclassManger();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["CID"]))
                {
                    BindCategoryName(Request.QueryString["CID"].ToString());
                    ShowCategory(Request.QueryString["CID"].ToString());
                    this.ListView6.DataSource = pm.Gethotpro();//最近销售
                    this.ListView6.DataBind();
                    BindCategoryProduct(Request.QueryString["CID"].ToString());
                    //this.rptCategoryProduct.DataSource = PM.GetProductInfoByFirstCategory(new Guid(Request.QueryString["CID"].ToString()));
                    //this.rptCategoryProduct.DataBind();
                    Bind2();
                }
            }
        }

        private void Bind2()
        {
            List<articleclass> classes = articleClassManager.GetAll();
            articleclass class2 = classes.Where(c => c.name == "活动新闻").Single();

            this.rptNews.DataSource = articlemanger.GetAlls(class2.id).Take(5);
            this.rptNews.DataBind();
        }

        private void BindCategoryProduct(string categoryid)
        {
            rptCategoryProduct.DataSource = this.m_CategoryManager.GetSubCateogoriesBySupCategory(this.m_CategoryManager.GetCategoryById(new Guid(categoryid))).OrderBy(theCategory => theCategory.Index);
            rptCategoryProduct.DataBind();
        }

        private void BindCategoryName(string categoryid)
        {
            this.ltlCategoryName.Text = m_CategoryManager.GetCategoryById(new Guid(categoryid)).Name;
            //title
            //this.Page.Title = string.Format("【{0}频道】日花商城－尽享万千正品医疗器械,医用耗材网上采购新体验!", this.ltlCategoryName.Text);
            //this.Page.MetaDescription = string.Format("日花商城{0}频道,轻松拥有性价比更高的{0}产品,商城100%正品保证,全国联保,积分兑大礼,网购{0}产品,首选日花商城【直销,便捷,诚信！】", this.ltlCategoryName.Text);

            this.Page.Title = string.Format("{0}_日花商城|我们最懂医疗保健器械", this.ltlCategoryName.Text);
            this.Page.MetaDescription = string.Format("日花商城是国内领先的家用医疗保健器械供应商,为您提供最全最好的{0}产品,全场商品最低2折起售,全国联保！网购{0}产品我信赖日花商城！", this.ltlCategoryName.Text);
            this.Page.MetaKeywords = this.ltlCategoryName.Text;
        }

        private void ShowCategory(string categoryid)
        {
            List<Category> subCategorys = this.m_CategoryManager.GetSubCateogoriesBySupCategory(this.m_CategoryManager.GetCategoryById(new Guid(categoryid))).OrderBy(theCategory => theCategory.Index).ToList();
            string subs = string.Empty;

            foreach (Category item in subCategorys)
            {
                subs += item.Name + ",";
            }
            subs += "日花商城";
            //keywords
            
            //this.Page.MetaKeywords = subs; //"家用医疗,检测诊断,日常护理,保健治疗,轮椅助行,制氧雾化,日花商城";
            
            rptCategory.DataSource = subCategorys;
            rptCategory.DataBind();
        }

        protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hfCategoryId = e.Item.FindControl("hfCagegoryId") as HiddenField;
            Repeater rpt = e.Item.FindControl("rptSubCategory") as Repeater;

            rpt.DataSource = this.m_CategoryManager.GetSubCateogoriesBySupCategory(this.m_CategoryManager.GetCategoryById(new Guid(hfCategoryId.Value))).OrderBy(theCategory => theCategory.Index);
            rpt.DataBind();
        }
        protected void rptProductByCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lbprice = e.Item.FindControl("lbePrice") as Label;
            Image img = e.Item.FindControl("Image9") as Image;

            //hfePrice.Value != "" && decimal.Parse(hfePrice.Value) == decimal.Parse("0.01")
            if (lbprice.Text != "" && decimal.Parse(lbprice.Text) == decimal.Parse("0.01"))
            {
                img.Visible = true;
                lbprice.Visible = false;
            }
            else
            {
                img.Visible = false;
                lbprice.Visible = true;
            }
        }


        protected void rptSubCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hfCategoryId = e.Item.FindControl("hfCagegoryId") as HiddenField;
            HyperLink hlcategory = e.Item.FindControl("HLSubCategoryId") as HyperLink;
            
            string upCategoryId = m_CategoryManager.GetCategoryById(new Guid(hfCategoryId.Value)).SupCategoryId.ToString();

            hlcategory.NavigateUrl = "~/Product/ProductCategory.aspx?CID=" + upCategoryId + "&SID=" + hfCategoryId.Value;
        }
        public string spliteStr(string title)
        {
            if (title.Length > 10)
            {
                title = title.Substring(0, 10) + "…";
            }
            return title;
        }
        public string spliteStrs(string title)
        {
            if (title.Length > 15)
            {
                title = title.Substring(0, 15) + "…";
            }
            return title;
        }

        protected void rptCategoryProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hfCategoryId = e.Item.FindControl("hfCagegoryId") as HiddenField;
            Repeater rpt = e.Item.FindControl("rptProductByCategory") as Repeater;

            //PM.GetProductByFirstCategory

            rpt.DataSource = PM.GetProductByFirstCategory(new Guid(hfCategoryId.Value)).Take(4);
            rpt.DataBind();
        }
    }
}